# -*- python -*-
# ex: set syntax=python:

# <pep8 compliant>

# Dictionary that the buildmaster pays attention to.
c = BuildmasterConfig = {}

# BUILD SLAVES
#
# We load the slaves and their passwords from a separator file, so we can have
# this one in SVN.

from buildbot.buildslave import BuildSlave
import master_private

c['slaves'] = []

for slave in master_private.slaves:
    c['slaves'].append(BuildSlave(slave['name'], slave['password']))

# TCP port through which slaves connect

c['slavePortnum'] = 9989

# CHANGE SOURCES

from buildbot.changes.svnpoller import SVNPoller

c['change_source'] = SVNPoller(
       'https://svn.blender.org/svnroot/bf-blender/trunk/',
       pollinterval=1200)

# BUILDERS
#
# The 'builders' list defines the Builders, which tell Buildbot how to perform a build:
# what steps, and which slaves can execute them.  Note that any particular build will
# only take place on one slave.

from buildbot.process.factory import BuildFactory
from buildbot.steps.source import SVN
from buildbot.steps.shell import ShellCommand
from buildbot.steps.shell import Compile
from buildbot.steps.shell import Test
from buildbot.steps.transfer import FileUpload
# from buildbot.steps.transfer import FileDownload
from buildbot.steps.master import MasterShellCommand
from buildbot.config import BuilderConfig

# add builder utility

c['builders'] = []
buildernames = []


def add_builder(c, name, libdir, factory):
    slavenames = []

    for slave in master_private.slaves:
        if name in slave['builders']:
            slavenames.append(slave['name'])

    if len(slavenames) > 0:
        f = factory(name, libdir)
        c['builders'].append(BuilderConfig(name=name, slavenames=slavenames, factory=f, category='blender'))
        buildernames.append(name)

# common steps


def svn_step():
    return SVN(baseURL='https://svn.blender.org/svnroot/bf-blender/%%BRANCH%%/blender', mode='update', defaultBranch='trunk', workdir='blender')


def lib_svn_step(dir):
    return SVN(name='lib svn', baseURL='https://svn.blender.org/svnroot/bf-blender/%%BRANCH%%/lib/' + dir, mode='update', defaultBranch='trunk', workdir='lib/' + dir)

# generic builder


def generic_builder(id, libdir=""):
    filename = 'buildbot_upload_' + id + '.zip'
    compile_script = '../blender/build_files/buildbot/slave_compile.py'
    test_script = '../blender/build_files/buildbot/slave_test.py'
    pack_script = '../blender/build_files/buildbot/slave_pack.py'
    unpack_script = 'master_unpack.py'

    f = BuildFactory()
    f.addStep(svn_step())
    if libdir != '':
        f.addStep(lib_svn_step(libdir))

    f.addStep(Compile(command=['python', compile_script, id]))
    f.addStep(Test(command=['python', test_script, id]))
    f.addStep(ShellCommand(name='package', command=['python', pack_script, id], description='packaging', descriptionDone='packaged'))
    if id.find('cmake') != -1:
        f.addStep(FileUpload(name='upload', slavesrc='buildbot_upload.zip', masterdest=filename, maxsize=100 * 1024 * 1024))
    else:
        f.addStep(FileUpload(name='upload', slavesrc='buildbot_upload.zip', masterdest=filename, maxsize=100 * 1024 * 1024, workdir='install'))
    f.addStep(MasterShellCommand(name='unpack', command=['python', unpack_script, filename], description='unpacking', descriptionDone='unpacked'))
    return f

# builders

add_builder(c, 'mac_x86_64_cmake', 'darwin-9.x.universal', generic_builder)
add_builder(c, 'mac_i386_cmake', 'darwin-9.x.universal', generic_builder)
add_builder(c, 'mac_ppc_cmake', 'darwin-9.x.universal', generic_builder)
add_builder(c, 'linux_x86_64_cmake', '', generic_builder)
add_builder(c, 'linux_x86_64_scons', '', generic_builder)
add_builder(c, 'win32_scons', 'windows', generic_builder)

# SCHEDULERS
#
# Decide how to react to incoming changes.

# from buildbot.scheduler import Scheduler
from buildbot.schedulers import timed

c['schedulers'] = []
#c['schedulers'].append(Scheduler(name="all", branch=None,
#                                 treeStableTimer=None,
#                                 builderNames=[]))
#c['schedulers'].append(timed.Periodic(name="nightly",
#                        builderNames=buildernames,
#                        periodicBuildTimer=24*60*60))

c['schedulers'].append(timed.Nightly(name='nightly',
    builderNames=buildernames,
    hour=3,
    minute=0))

# STATUS TARGETS
#
# 'status' is a list of Status Targets. The results of each build will be
# pushed to these targets. buildbot/status/*.py has a variety to choose from,
# including web pages, email senders, and IRC bots.

c['status'] = []

from buildbot.status import html
from buildbot.status.web import authz

authz_cfg = authz.Authz(
    # change any of these to True to enable; see the manual for more
    # options
    gracefulShutdown=False,
    forceBuild=True,  # use this to test your slave once it is set up
    forceAllBuilds=False,
    pingBuilder=False,
    stopBuild=False,
    stopAllBuilds=False,
    cancelPendingBuild=False,
)

c['status'].append(html.WebStatus(http_port=8010, authz=authz_cfg))

# PROJECT IDENTITY

c['projectName'] = "Blender"
c['projectURL'] = "http://www.blender.org"

# the 'buildbotURL' string should point to the location where the buildbot's
# internal web server (usually the html.WebStatus page) is visible. This
# typically uses the port number set in the Waterfall 'status' entry, but
# with an externally-visible host name which the buildbot cannot figure out
# without some help.

c['buildbotURL'] = "http://builder.blender.org/"

# DB URL
#
# This specifies what database buildbot uses to store change and scheduler
# state.  You can leave this at its default for all but the largest
# installations.

c['db_url'] = "sqlite:///state.sqlite"
